<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>档案移交添加</title>
    <script src="/scripts/boot.js"></script>
</head>
<body class="bg-white">
<div id="addForm">
    <table class="editTable table-bg" style="width:100%;table-layout:fixed;" border="0" cellspacing="1" cellpadding="1">
        <colgroup>
            <col width="130px"></col>
            <col width="50%"></col>
            <col width="130px"></col>
            <col width="50%"></col>
        </colgroup>
        <tr>
            <td class="text-titlebg">计划名称</td>
            <td>
                <input type="hidden" class="mini-hidden" name="id"/>
                <div class="input-div color-66" id="planName"></div>
                <!--<input name="planName" class="mini-textbox w220" readonly="readonly" vtype="maxLength:50" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"/></select>-->
            </td>
            <td class="text-titlebg">执行人</td>
            <td>
                <div class="input-div color-66" id="exeName"></div>
                <!--<input name="exeName" class="mini-textbox w220" readonly="readonly" vtype="maxLength:50" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"/></select>-->
            </td>
        </tr>
        <tr>
            <td class="text-titlebg noEmpty w-166px">计划状态</td>
            <td colspan="3">
                <div class="input-div color-66" id="planState"></div>
                <!--<input name="state" id="state" class="mini-textbox w220" readonly="readonly" vtype="maxLength:50" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"/>-->
            </td>
        </tr>
        <tr>
            <td class="text-titlebg noEmpty">问题描述</td>
            <td colspan="3">
                <div class="textarea-div color-66" id="problem"></div>
                <!--<textarea id="problem" name="problem" class="mini-textarea wtextarea" required="true" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"></textarea>-->
            </td>
        </tr>
     <tr>
            <td class="text-titlebg noEmpty">备注</td>
            <td colspan="3" class="wp">
                <textarea id="remark" name="remark" class="mini-textarea wtextarea" maxlength='500'  required="true" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"></textarea>
            </td>
        </tr>
    </table>
    
    <div class="attachment-box">
        <div class="attachment-title">
            <div class="attachment-text">
                <span class="blue-dot"></span>附件
            </div>
        </div>
        <div class="attachment-content">
            <div id="uploader" class="webuploader-container"></div>
            <div class="attachment-content">
		    		<div id="file_ll" class="updata-file"></div>
				</div>
            <input name="fileUrl" id="file_url" class="mini-hidden"  />
            <input name="fileName" id="file_name" class="mini-hidden" />
        </div>
    </div>
    <div class="title-info"><span>督导方案</span>
        <div class="position-rel">
            <div class="position-abs">
                <!--<a onclick="view.addRow()" class="newly" iconCls="icon-upload">新增</a>-->
                <!--<a class="delAll" onclick="view.removeRow()">删除</a>-->
            </div>
        </div>
    </div>
    <div id="datagrid1" class="mini-datagrid" style="width:100%;height:280px; margin-top: 35px;" idField="id"
          showPager="true" allowCellSelect="true" multiSelect="true" editNextOnEnterKey="true"  editNextRowCell="true">
        <div property="columns">
            <!--<div type="checkcolumn"></div>-->
            <div type="indexcolumn" headerAlign="center" width="20">编号</div>
            <div field="scheme" headerAlign="center" name="scheme" width="100" allowSort="true" >方案名称
                <input property="editor" class="mini-textbox"  minValue="0" maxValue="200" value="25" style="width:100%;" />
            </div>
            <!--<div name="action" width="80" headerAlign="center" allowSort="true">操作</div>-->
        </div>
    </div>
    <div class="chose-toolbar">
        <a class="chose-button" onclick="CloseWindow('close')">取消</a>
    </div>
</div>
<script type="text/javascript">
    var grid,form,datas;
    var view = {
        init: function () {
            mini.parse();
            form = new mini.Form("#addForm");
            grid = mini.get("datagrid1");
            // grid.load();
            grid.on("celleditenter", function (e) {
                var index = grid.indexOf(e.record);
                if (index == grid.getData().length - 1) {
                    var row = {};
                    grid.addRow(row);
                }
            });

            grid.on("beforeload", function (e) {
                if (grid.getChanges().length > 0) {
                    if (confirm("有增删改的数据未保存，是否取消本次操作？")) {
                        e.cancel = true;
                    }
                }
            });

            grid.on(
                "drawcell",
                function(e) {
                    var column = e.column;
                    var record = e.record;
                    if (column.name == "action") {
                        e.cellStyle = "text-align:center";
                        e.cellHtml = '<a class="del-botton" href="javascript:view.delRows()">删除</a>'
                    }

                });
            grid.on("celleditenter", function(e) {
                var index = grid.indexOf(e.record);
                if (index == grid.getData().length - 1) {
                    var row = {};
                    grid.addRow(row);
                }
            });

            grid.on("beforeload", function(e) {
                if (grid.getChanges().length > 0) {
                    if (confirm("有增删改的数据未保存，是否取消本次操作？")) {
                        e.cancel = true;
                    }
                }
            });
        },
        //添加行
        addRow: function () {
            var row = {};
            grid.addRow(row, 0);
            grid.cancelEdit();
            grid.beginEditRow(row);
        },
        //删除行
        removeRow: function () {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        },
        //保存
        save: function (status) {
            form.validate();
            var infoss = grid.data;
            var arr = '';
            if(infoss.length>1){
                for(i in infoss){
                    var info = infoss[i];
                    var  scheme =  info.scheme;
                    if(scheme!=undefined){
                        arr += scheme+'@_@';
                    }
                }
                arr = arr.substring(0, arr.lastIndexOf('@_@'));
            }else if(infoss.length==1){
                arr = infoss[0].scheme
            }
            if (form.isValid() == false) return;
            var data = form.getData();      //获取表单多个控件的数据
            data.scheme=arr;
            data.status=status;
            data.planNodeId=data.id;
            delete data["id"];
            $.ajax({
                type : "post",
                url : "/supervise/problem/add",
                data : data,
                success : function(msg) {
                    CloseWindow("ok")
                }
            });
        },
        //提交单元格编辑数据前激发
        onCellCommitEdit: function (e) {
            var editor = e.editor;

            editor.validate();
            if (editor.isValid() == false) {
                alert(editor.getErrorText());
                e.cancel = true;
            }
        },
        // 设置行
        setRows: function (rows) {
            var gridData = grid.getData();
            for (var i = 0; i < gridData.length; i++) {
                for (var j = 0; j < rows.length; j++) {
                    if (gridData[i].id == rows[j].id) {
                        delete rows[j]
                    }
                }
            }

            grid.addRows(rows);
        },
        onDrawCell: function(e) {
            var record = e.record;

            if (e.field == "total") {
                var price = record.unitPrice;
                var quantity = record.purchaseQuantity;
                var total = price * quantity;
                if (isNaN(total)) {
                    total = 0;
                }

                e.cellHtml = total;
            }
        },
        onDrawSummaryCell: function(e) {
            var result = e.result;
            var grid = e.sender;
            var rows = e.data;

            if (e.field == "total") {
                var total = 0;
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    var t = row.unitPrice * row.purchaseQuantity;
                    if (isNaN(t))
                        continue;
                    total += t;
                }
                mini.get('planningTotalAmount').setValue(total)
            }
        },
        //删除行
        delRows: function() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        },
        SetData:function (data) {
            data = mini.clone(data);
            $.ajax({
                url:"/supervise/problem/info?id=" + data.id,
                type: "post",
                success: function (text) {
                    console.log(text)
                    //跨页面传递的数据对象，克隆后才可以安全使用
                    datas = mini.decode(text);   //反序列化成对象
                    
                    if(text){
                        var div = $("#file_ll");
                        var listFile = text.listFile;
                        for(var i=0;i<listFile.length;i++ ){
                            var file = listFile[i];
                            var fileid = file.id;
                            var a = "<div class='file-box' id='" + file.id + "'>"+
        	    			"<a class='file-name' onclick=\"downFile('" +file.fileUrl+"','"+file.fileName+"')\">" + file.fileName + "</a>"+
        	    			// "<span class='img-wh' onclick=\"view.delFile('" +fileid+"','"+file.fileUrl+"')\"></span>"+
            				"</div>";
                            div.append(a);

                        }
                    }
                    $.each(datas,function (key,value) {
                        $("#"+key).html(value);
                        if(key=="planState"){
                            var data;
                            if(value==3){
                                data = "有风险";
                            }else if(value==4){
                                data = "失控";
                            }else {

                            }
                            $("#planState").html(data)
                        }
                    });
                    grid.setData(datas.listsp)
                }
            });
        }
    };
    view.init();
</script>
</body>
</html>